﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Mvc;

using Hangfire.AppDomain.Demo.Models;

using Microsoft.Ajax.Utilities;

using Newtonsoft.Json;

using Seasky.AssemblyLoader;

namespace Hangfire.AppDomain.Demo.Controllers
{
    public class HomeController : Controller
    { 
        public ActionResult Index()
        {
            /* string className = "Test.TestCla";
             string dllName = "Test";
             string funName1 = "saidHello";
             string funName2 = "saidBye";

             string assemblyDir = System.AppDomain.CurrentDomain.BaseDirectory + @"plugin\";//   Test.dll
             var files = GetAllFiles(assemblyDir);
             foreach (var item in files)
             {
                 if (item.FileName.Equals(dllName))
                 {
                     *//* ViewBag.SayHello = GetString(item.FileFullName, funName1, className); 
                      ViewBag.SayBye = GetString(item.FileFullName, funName2, className); *//*
                      RecurringJob.AddOrUpdate(() => GetString(item.FileFullName, funName1, className), "*//*5 * * * * *");

                 }
             }
             */
            return View();
        }
        public ActionResult AddJob(string className, string dllName, string funName,string cron)
        {
            string assemblyDir = System.AppDomain.CurrentDomain.BaseDirectory + @"plugin\";//   Test.dll
            var files = GetAllFiles(assemblyDir);
            foreach (var item in files)
            {
                if (item.FileName.Equals(dllName))
                { 
                     RecurringJob.AddOrUpdate($"{className}.{funName}", () => GetString(item.FileFullName, funName, className), cron);

                }
            }
            return Content("执行成功");
        }

        public ActionResult RemoveJob(string className, string dllName, string funName)
        {
            string assemblyDir = System.AppDomain.CurrentDomain.BaseDirectory + @"plugin\";//   Test.dll
            var files = GetAllFiles(assemblyDir);
            foreach (var item in files)
            {
                if (item.FileName.Equals(dllName))
                {
                    RecurringJob.RemoveIfExists($"{className}.{funName}");

                }
            }
            return Content("删除成功");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

       
     

        /// <summary>
        /// 作为遍历文件函数的子函数
        /// </summary>
        /// <param name="parentDir">目录名称</param>
        /// <param name="result">该目录下的所有文件</param>
        public List<FileDto> GetAllFiles(string parentDir)
        {
            
            //获取目录parentDir下的所有的文件，并过滤得到所有的文本文件
            //var files = Directory.GetFiles(parentDir, "*.dll");
            DirectoryInfo folder = new DirectoryInfo(parentDir);
            var files = folder.GetFiles("*.dll");
            List<FileDto> result = new List<FileDto>();
            foreach (var file in files)
            {
                FileDto fileDto = new FileDto();
                fileDto.FileFullName = file.FullName;
                fileDto.FileName = file.Name.Substring(0, file.Name.Length-4);
                fileDto.FileSuffix = "dll";
                result.Add(fileDto);
            }  
            return result;
        }

        public string GetString(string fileFullName,string funName,string className)
        {
            Loader loader = new Loader();
            string result = loader.InvokeMethod<string>(fileFullName, funName, null, className);
            loader.Unload(fileFullName);
            ResultModel resultModel = JsonConvert.DeserializeObject<ResultModel>(result);
            return resultModel?.SaidSomething;
        }
    }
}